<template>
  <div class="comment-reply">
    <van-nav-bar
      :title="
        comment.reply_count > 0 ? `${comment.reply_count}条回复` : '暂无回复'
      "
    >
      <van-icon name="cross" slot="left" @click="$emit('close')"></van-icon>
    </van-nav-bar>
    <div class="scroll-wrap">
      <!-- 当前评论项开始 -->
      <comment-item :comment="comment"></comment-item>
      <!-- 当前评论项结束 -->
      <!-- 评论的回复列表开始 -->
      <van-cell title="全部回复" />
      <comment-list
        :source="comment.com_id"
        type="c"
        :list="commentList"
      ></comment-list>
      <!-- 评论的回复列表结束 -->
    </div>
    <div class="reply-wrap">
      <van-button class="postBtn" size="small" round @click="isPostShow = true"
        >发布评论</van-button
      >
    </div>
    <!-- 发布评论弹窗开始 -->
    <van-popup v-model="isPostShow" position="bottom">
      <comment-post
        :target="comment.com_id"
        @replaySuccess="onPostSuccess"
      ></comment-post>
    </van-popup>
    <!-- 发布评论弹窗结束 -->
  </div>
</template>

<script>
import CommentItem from './comment-item'
import CommentList from './comment-list'
import CommentPost from './comment-post'
export default {
  name: 'CommentReply',
  data() {
    return {
      isPostShow: false,
      commentList: [] // 评论的回复列表
    }
  },
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  components: {
    CommentItem,
    CommentList,
    CommentPost
  },
  methods: {
    onPostSuccess(data) {
      // 更新回复的数量
      this.comment.reply_count++
      // 关闭弹层
      this.isPostShow = false
      // 将最新回复的内容渲染在页面上
      this.commentList.unshift(data.new_obj)
    }
  }
}
</script>

<style scoped lang="less">
.scroll-wrap {
  position: fixed;
  top: 92px;
  left: 0;
  right: 0;
  bottom: 88px;
  overflow-y: auto;
}
.reply-wrap {
  height: 88px;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fff;
  border-top: 1px solid #d8d8d8;
  .postBtn {
    width: 60%;
  }
}
</style>
